/*
 * NativeBlas.cs
 *
 * Wrapper around dnA.Native.IBlas.
 *
 * Copyright (c) 2005, dnAnalytics. All rights reserved.
 */
using dnAnalytics.LinearAlgebra.Native;

namespace dnAnalytics.LinearAlgebra
{
    internal sealed class NativeBlas : ISimplifiedBlas
    {
        private IBlas blas = Provider.BlasInstance;

        public int AbsoluteMaximumIndex(double[] data)
        {
            return blas.Idamax(data.Length, data, 1);
        }

        public double SumMagnitudes(double[] data)
        {
            return blas.Dasum(data.Length, data, 1);
        }

        public double DotProduct(double[] x, double[] y)
        {
            return blas.Ddot(x.Length, x, 1, y, 1);
        }

        public void Scale(double constant, double[] data)
        {
            blas.Dscal(data.Length, constant, data, 1);
        }

        public void Subtract(double[] vector, double constant)
        {
            double[] temp = new double[vector.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = constant;
            }
            blas.Daxpy(vector.Length, -1, temp, 1, vector, 1);
        }

        public void Subtract(double[] x, double[] y)
        {
            blas.Daxpy(x.Length, -1, y, 1, x, 1);
        }

        public void Add(double[] vector, double constant)
        {
            double[] temp = new double[vector.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = constant;
            }

            blas.Daxpy(vector.Length, 1, temp, 1, vector, 1);
        }

        public void Add(double[] x, double[] y)
        {
            blas.Daxpy(x.Length, 1, y, 1, x, 1);
        }

        public void Multiply(int m, int n, int k, double[] x, double[] y, double[] result)
        {
            blas.Dgemm(BlasOrderType.Column, BlasTransType.NoTrans, BlasTransType.NoTrans, m, n, k, 1, x, m, y, k, 0, result, m);
        }
    }
}